package com.version1_4.service.implement;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.format.ReevesMap;
import com.util.format.TimeFormat;
import com.version1_4.dao.MsgDao;
import com.version1_4.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by reeveslee on 16/5/7.
 */
@Service
public class MsgServiceImpl implements MsgService {
    @Autowired
    private MsgDao msgDao;

    /**
     * 回复消息
     *
     * @param userId
     * @param lastId
     * @return
     */
    @Override
    public String topicReplyMsg(int userId, int lastId) {
        ReevesMap reevesMap = new ReevesMap();
        List<Map<String, Object>> topicReplyMsgList = msgDao.topicReplyMsg(userId, lastId);
        Date lastReplyReadTime = msgDao.lastTopicReplyTime(userId);
        boolean hasReply = false;

        // 回复消息
        for (Map<String, Object> topicReplyMsg : topicReplyMsgList) {
            topicReplyMsg.replace("image", JSONArray.parse((String) topicReplyMsg.get("image")));
            topicReplyMsg.replace("feedImage", JSONArray.parse((String) topicReplyMsg.get("feedImage")));
            topicReplyMsg.replace("feedVoice", JSONObject.parse((String) topicReplyMsg.get("feedVoice")));
            topicReplyMsg.replace("voice", JSONObject.parse((String) topicReplyMsg.get("voice")));

            if (lastReplyReadTime.getTime() < ((Date) topicReplyMsg.get("time")).getTime()) {
                hasReply = true;
                topicReplyMsg.put("isNew", true);
            } else {
                topicReplyMsg.put("isNew", false);
            }
            topicReplyMsg.replace("time", TimeFormat.convertTimeToFormat(((Date) topicReplyMsg.get("time")).getTime() / 1000));
        }

        msgDao.freshLastTopicReplyTime(userId);

        reevesMap.put("replyMsg", topicReplyMsgList);
        reevesMap.put("hasReply", hasReply);

        if (lastId == 0) {
            // 点赞消息
            List<Map<String, Object>> topicPraiseMsgList = msgDao.topicPraiseMsg(userId, 0);
            Date lastPraiseReadTime = msgDao.lastTopicPraiseTime(userId);
            boolean hasPraise = false;

            for (Map<String, Object> topicPraiseMsg : topicPraiseMsgList) {
                topicPraiseMsg.replace("feedImage", JSONArray.parse((String) topicPraiseMsg.get("feedImage")));
                topicPraiseMsg.replace("feedVoice", JSONObject.parse((String) topicPraiseMsg.get("feedVoice")));

                if (lastPraiseReadTime.getTime() < ((Date) topicPraiseMsg.get("time")).getTime()) {
                    hasPraise = true;
                    topicPraiseMsg.put("isNew", true);
                } else {
                    topicPraiseMsg.put("isNew", false);
                }
                topicPraiseMsg.replace("time", TimeFormat.convertTimeToFormat(((Date) topicPraiseMsg.get("time")).getTime() / 1000));
            }

            msgDao.freshLastTopicPraiseTime(userId);
            reevesMap.put("praiseMsg", topicPraiseMsgList);
            reevesMap.put("hasPraise", hasPraise);
        }

        return net.sf.json.JSONObject.fromObject(reevesMap).toString();
    }

    @Override
    public String topicPraiseMsg(int userId, int lastId) {
        ReevesMap reevesMap = new ReevesMap();

        // 点赞消息
        List<Map<String, Object>> topicPraiseMsgList = msgDao.topicPraiseMsg(userId, lastId);
        Date lastPraiseReadTime = msgDao.lastTopicPraiseTime(userId);
        boolean hasPraise = false;

        for (Map<String, Object> topicPraiseMsg : topicPraiseMsgList) {
            topicPraiseMsg.replace("feedImage", JSONArray.parse((String) topicPraiseMsg.get("feedImage")));
            topicPraiseMsg.replace("feedVoice", JSONObject.parse((String) topicPraiseMsg.get("feedVoice")));

            if (lastPraiseReadTime.getTime() < ((Date) topicPraiseMsg.get("time")).getTime()) {
                hasPraise = true;
                topicPraiseMsg.put("isNew", true);
            } else {
                topicPraiseMsg.put("isNew", false);
            }
            topicPraiseMsg.replace("time", TimeFormat.convertTimeToFormat(((Date) topicPraiseMsg.get("time")).getTime() / 1000));
        }

        msgDao.freshLastTopicPraiseTime(userId);
        reevesMap.put("praiseMsg", topicPraiseMsgList);
        reevesMap.put("hasPraise", hasPraise);
        return net.sf.json.JSONObject.fromObject(reevesMap).toString();
    }
}
